﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShippingID.Models;
using BL;

namespace ShippingID.Controllers
{
    public class CaminhaoController : Controller
    {
        //
        // GET: /Caminhao/
        private ServiceGeral _service;
        public CaminhaoController()
        {
            _service = new ServiceGeral();

        }
        public ActionResult Index()
        {
            return View(_service.RetornarTodos<Modelo.Caminhao>());
        }

        //
        // GET: /Caminhao/Details/5

        public ActionResult Details(int id)
        {
            return View(_service.RetornarPorID<Modelo.Caminhao>(id));
        }

        //
        // GET: /Caminhao/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Caminhao/Create

        [HttpPost]
        public ActionResult Create(Modelo.Caminhao Caminhao)
        {
            //if (TryUpdateModel(product))
            //{
            //    //The model is valid - insert the product and redisplay the grid.
            //    SessionProductRepository.Insert(product);
            //    //GridRouteValues() is an extension method which returns the 
            //    //route values defining the grid state - current page, sort expression, filter etc.
            //    RouteValueDictionary routeValues = this.GridRouteValues();
            //    // add the editing mode to the route values
            //    routeValues.Add("mode", mode);
            //    return RedirectToAction("EditingServerSide", routeValues);
            //}
            ////The model is invalid - render the current view to show any validation errors
            //return View("EditingServerSide", SessionProductRepository.All());



            if (TryValidateModel(Caminhao))
            {
                _service.Gravar<Modelo.Caminhao>(Caminhao);
                return RedirectToAction("Index");
            }

            return View("EditingServerSide", _service.RetornarTodos<Modelo.Caminhao>());
        }

        //
        // GET: /Caminhao/Edit/5

        public ActionResult Edit(int id)
        {
            return View(_service.RetornarPorID<Modelo.Caminhao>(id));
        }

        //
        // POST: /Caminhao/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Modelo.Caminhao Caminhao)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _service.Alterar<Modelo.Caminhao>(Caminhao);
                    return RedirectToAction("Index");
                }
                return View(Caminhao);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Caminhao/Delete/5

        public ActionResult Delete(int id)
        {
            return View(_service.RetornarPorID<Modelo.Caminhao>(id));
        }

        //
        // POST: /Caminhao/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            _service.Excluir<Modelo.Caminhao>(id);
            return RedirectToAction("Index");
        }
    }
}